package com.hbjycl.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hbjycl.entity.Bbs;
import com.hbjycl.entity.Comment;
import com.hbjycl.entity.Tree;
import com.hbjycl.service.BaseService;
import com.hbjycl.service.BbsService;
import com.hbjycl.service.CommentService;

@Controller
public class BbsController extends BaseController
{
	 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BbsController.class);
 
	@Resource BbsService bbsservice;
	@Resource CommentService commentservice;
    @RequestMapping(value = "/bbslist", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        List<Bbs> bbslist=bbsservice.getBbsBylasttime();
        mav.addObject("bbslist", bbslist);
        mav.setViewName("bbs/bbslist");
        return mav;
        }
    
    
    /*
     * 课件添加页面
     */
    @RequestMapping(value = "/bbsaddview", method = RequestMethod.GET)
    public ModelAndView courseaddview(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("bbs/bbsadd");
        return mav;
        }
    /*
     * 课件添加
     */
    @RequestMapping(value = "/bbsadd", method = RequestMethod.POST)
    public ModelAndView bbsadd(
   	       @RequestParam(value = "bbsname", required = true) String bbsname,
   		 HttpSession session, HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("bbs/bbsadd");
        Bbs bbs=new Bbs();
        bbs.setBbsname(bbsname);
        bbs.setClicknum(0);
        bbs.setCommentnum(0);
        bbs.setCreateDate(new Date());
        bbs.setLasttime(new Date());
        if(session!=null)
    	{
            mav.addObject("username", session.getAttribute("username"));
            bbs.setPublisher((String) session.getAttribute("username"));
    	}
        try{
        bbsservice.save(bbs);
        List<Bbs> bbslist=bbsservice.getBbsBylasttime();
        mav.addObject("bbslist", bbslist);
        mav.setViewName("bbs/bbslist");
        }
        catch(Exception e){
        	mav.addObject(TIP_ERROR, e);
        }
        return mav;
        }
  
    @RequestMapping(value = "/commentlist", method = RequestMethod.GET)
    public ModelAndView commentlist(
   		 HttpSession session, HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String bbsid=request.getParameter("bbsid");      
        Bbs bbs=bbsservice.get(bbsid);
        List<Comment> commentlist=bbsservice.getCommentList(bbs.getBbsname());
        //点击数+1
        if("true".equals(request.getParameter("isclick")))
        {
        bbs.setClicknum(bbs.getClicknum()+1);
        bbsservice.update(bbs);
        }          
        mav.addObject("bbs", bbs);
        mav.addObject("commentlist", commentlist);
        mav.setViewName("bbs/commentlist");
        return mav;
        }
    
    @RequestMapping(value = "/commentadd", method = RequestMethod.GET)
    public ModelAndView commentadd(
   		 HttpSession session, HttpServletRequest request){
        ModelAndView mav=new ModelAndView();  
        Comment newcomment=new Comment();
        newcomment.setCommentstr(request.getParameter("commentstr"));
        newcomment.setCreateDate(new Date());
        if(session!=null)
    	{
            mav.addObject("username", session.getAttribute("username"));
           newcomment.setCommentuser((String) session.getAttribute("username"));
    	}
        String bbsname=request.getParameter("bbsname");
        newcomment.setBbsname(bbsname);
        Bbs bbs=bbsservice.getBbsBybbsname(bbsname); 
        List<Comment> commentlist=bbsservice.getCommentList(bbs.getBbsname());
        try{
        	commentservice.save(newcomment);        
            //更新评论数
            bbs.setCommentnum(commentlist.size());
            //更新最近评论时间
            bbs.setLasttime(new Date());
            bbsservice.update(bbs);
            mav.addObject("bbs", bbs);
        }
        catch(Exception e){
        	mav.addObject(TIP_ERROR, e);
        }
        mav.addObject("bbs", bbs);
        mav.addObject("commentlist", commentlist);
        mav.setViewName("redirect:commentlist?bbsid="+bbs.getId());
        return mav;
        }
   
}
